<template>
  <div class="layout-pd">
    <zh-space>
      <zh-card title="" v-if="pageData.showType">
        <zh-form :model="pageData.queryParams" ref="queryFormRef" label-width="160px" :span="8">
          <zh-form-input prop="labelCode" label="标签编码" placeholder="请输入标签编码" v-model="pageData.queryParams.labelCode"></zh-form-input>
		      <zh-form-input prop="fieldName" label="标签名称" placeholder="请输入标签名称" v-model="pageData.queryParams.fieldName"></zh-form-input>
          <template v-if="pageData.advanced">
            <zh-form-select :disabled="pageData.labelTypeDisabled" prop="labelType" label="标签类型" placeholder="请输入标签类型" v-model="pageData.queryParams.labelType" :options="pageData.dictData.labelType"></zh-form-select>
            <zh-form-select prop="status" label="状态" placeholder="请选择" :options="pageData.statusDict" v-model="pageData.queryParams.status"></zh-form-select>
          </template>
          <zh-form-query v-model="pageData.advanced" :tableRef="queryTable" :formRef="queryFormRef"> 
            <zh-button button-type="add" @click="showAdd()" v-func:dictLabel0004>新增</zh-button>
          </zh-form-query>
        </zh-form>
      </zh-card>
      <zh-table title="" ref="queryTable" :toolbarConfig="pageData.toolbarConfig" size="mini" funcCode="dictLabel0001" :columns="pageData.tableColumns" highlight-current-row>
        <template #status_default="{ row }">
					<zh-badge :status="statusMap[row.status].status" :text="statusMap[row.status].text" />
				</template>
        <template #operate="{ row }">
          <zh-button v-func:dictLabel0002 link @click="showdetail(row, true)" style="color: green">查看</zh-button>
          <zh-button v-func:dictLabel0005 link @click="showdetail(row, false)">修改</zh-button>
          <zh-button v-func:dictLabel0006 link @click="deleteData(row)" style="color: red">删除</zh-button>
        </template>
      </zh-table>
    </zh-space>
    <zh-dialog style="width: 80%" :title="pageData.title" v-model="pageData.detailVisible">
      <dictLabelForm
          ref="detailFormRef"
          :disabled="pageData.showFlag"
          :labelType="pageData.labelType"
          @canner="canner"
          @refreshTable="refreshTable"
          :labelCode="pageData.selectRow.labelCode"
      ></dictLabelForm>
      <template #footer>
				<span class="dialog-footer">
					<zh-button @click="pageData.detailVisible = false" type="info">关闭</zh-button>
					<zh-button
              type="primary"
              :loading="pageData.saveLoading"
              :disabled="pageData.saveLoading"
              v-model:btnLoding="pageData.saveLoading"
              @click="save"
          >
						保存
					</zh-button>
				</span>
      </template>
    </zh-dialog>
  </div>
</template>
<script lang="ts" setup name="dictLabelIndex">
import { ElMessageBox, ElMessage } from 'element-plus';
import dictLabelForm from './component/dictLabelForm.vue';

// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onMounted(() => {
  let meta = useRouter().currentRoute.value.meta;
  if (meta && meta.query) {
    const query: any = meta.query;
    if (query.labelType) {
      pageData.queryParams.labelType = query.labelType;
      pageData.labelType = query.labelType;
      pageData.labelTypeDisabled = true;
    }
    
  }
  init();
});

// ------------------定义子组件--------------------------
const queryTable = ref();
const queryFormRef = ref();
const detailFormRef = ref();

const statusMap = {
  0: {
    status: 'error',
    text: '无效',
  },
  1: {
    status: 'processing',
    text: '有效',
  },
};

// ------------------变量区------------------------------
const pageData = reactive({
  advanced: false,
  labelTypeDisabled:false,
  labelType:'',
  statusDict: [
		{
			dictKey: '0',
			dictValue: '无效',
		},
		{
			dictKey: '1',
			dictValue: '有效',
		},
	],
  toolbarConfig: {
    custom: true,
  },
  tableColumns: [
    {
      type: 'seq',
      minWidth: 60,
      fixed: 'left',
      align: 'center',
      title: '序号',
    },
    {
      field: 'labelCode',
      title: '标签编码',
      visible: false,
      align: 'center',
      minWidth: 150,
    },
    {
      field: 'labelTypeName',
      title: '标签类型',
      visible: true,
      align: 'center',
      minWidth: 150,
    },
    {
      field: 'title',
      title: '标签名称',
      visible: true,
      align: 'center',
      minWidth: 300,
    },
    {
      field: 'parentCode',
      title: '父标签编码',
      visible: false,
      align: 'center',
      minWidth: 150,
    },
    {
      field: 'remarks',
      title: '描述',
      visible: true,
      align: 'center',
      minWidth: 300,
    },
    {
      field: 'status',
      title: '有效状态',
      visible: true,
      align: 'center',
      minWidth: 150,
      slots: {
				default: 'status_default',
			},
    },
    {
      field: 'operate',
      title: '操作',
      fixed: 'right',
      align: 'center',
      minWidth: 210,
      slots: {
        // 使用插槽模板渲染
        default: 'operate',
      },
    },
  ],
  queryParams: {} as any,
  dictData: {} as any,
  title: '',
  detailVisible: false,
  showFlag: false,
  selectRow: {} as any,
  saveLoading: false,
  showType: true,
  campusParam: {} as any,
});
// ------------------方法区------------------------------
const init = () => {
  getDict();
  refreshTable();
};
// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = 'labelType';
  $api.common.getBatchDict.post(params).then((res) => {
    pageData.dictData = res.result;
  });
};

//使用子组件方法，关闭弹窗
const canner = (e: any) => {
  pageData.detailVisible = e;
};

const showAdd = () => {
  pageData.title = '新增';
  pageData.detailVisible = true;
  pageData.showFlag = false;
  pageData.selectRow = {};
};

const showdetail = (row, flag) => {
  if (!flag) {
    pageData.title = '信息修改';
  } else {
    pageData.title = '查看详情';
  }
  pageData.selectRow = row;
  pageData.showFlag = flag;
  pageData.detailVisible = true;
};
//保存
const save = () => {
  detailFormRef.value.save();
};

//刷新
const refreshTable = () => {
  queryTable.value.refresh(pageData.queryParams);
};
//删除
const deleteData = (row) => {
  ElMessageBox.confirm('是否确认删除？').then(() => {
    let params = {} as any;
    params.labelCode = row.labelCode;
    $api.common.send.post('dictLabel0006', params).then(() => {
      ElMessage.success('删除成功');
      refreshTable();
    });
  });
};

</script>
<style lang="scss" scoped></style>
